<?php
include 'function.php';

$conn = conn();
$links = file('detail_links.txt');
foreach ($links as $i => $link)
{
	$link = trim($link);
	if (!empty($link))
	{
		$a = explode('&m=', $link);
		$m = $a[1];

		$sql = "SELECT id FROM films WHERE img = '".$m.".jpg'";
		$result = mysql_query($sql, $conn);
		if (!($row = mysql_fetch_assoc($result)))
		{
			file_put_contents('imgs/'.$m.'.jpg', file_get_contents('http://www.javnow.com/caps/'.$m.'/pic00004.jpg'));
		
			$sql = "UPDATE films SET img = '".$m.".jpg' WHERE id = $i + 1";
			mysql_query($sql, $conn);
		}

		$ch = curl_init('http://www.javnow.com/'.$link);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_REFERER, 'http://www.javnow.com');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($ch, CURLOPT_COOKIEFILE, '1.txt');   
		$result = curl_exec($ch);
		curl_close($ch);
		if (preg_match('/download\/[^"]+/', $result, $match)) 
		{
			$link = $match[0];
			$filmname = str_replace('download/', '', $link);
			$tmp = explode('.', $filmname);
			$filmname = $tmp[0].'.mp4';
			$sql = "UPDATE films SET name = '$filmname' WHERE id = $i + 1";
			mysql_query($sql, $conn);
		}
		else
			echo "no download link.\n";
	}
}

?>